package classes.view.component
{
	import classes.controller.EDataObject;
	import classes.controller.ENumber;
	
	import com.d4p.custom.customScrollBar.CustomScrollBar;
	import com.greensock.TweenMax;
	import com.greensock.easing.Cubic;
	import com.greensock.easing.Quart;
	
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.FocusEvent;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	import flash.text.TextFormat;
	
	public class PopupSelectBox extends Sprite
	{
		private var _num:int;
		private var _openCheck:Boolean;
		
		private var _listClip:MovieClip;
		private var _activetxt:TextField;
		private var _inputClip:MovieClip;
		private var _selectClip:MovieClip;
		private var _mask:Sprite;
		private var _colorNoChange:Boolean;
		
		private var _customScrollBar:CustomScrollBar;
		
		
		private var _isCreateRoomMode : Boolean; 
		
		public function PopupSelectBox()
		{
			super();
			setLayOut();
			init();
			defaultSetting();
		}
		
		private var _viewWidth:int;
		public function set viewWidth($num:Number):void
		{
			_viewWidth 					= $num;
			_inputClip.c_mc.width 		= $num - 9 - 6 - _inputClip.r_mc.width + 12;
			_inputClip.r_mc.x 			= _inputClip.c_mc.x + _inputClip.c_mc.width;
			_mask.width 				= $num + 10;
			_selectClip.bg_mc.width 	= $num - 2;
			_selectClip.mask_mc.width 	= $num - 2;
			
		}
		public function set boxDeem($chk:Boolean):void
		{
			if ($chk) {
				this.mouseChildren = false;
				this.mouseEnabled = false;
				TweenMax.to(_activetxt, 0, { alpha:.5, ease:Cubic.easeOut } );
			} else {
				this.mouseChildren = true;
				this.mouseEnabled = true;
				TweenMax.to(_activetxt, 0, { alpha:1, ease:Cubic.easeOut } );
			}
		}
		public function set firstTitle($title:String):void
		{
			this._activetxt.text = $title;
		}
		
		private function scrollbarReset():void
		{
			//스크롤바 업뎃.
			this.addEventListener(Event.ADDED_TO_STAGE,	onStageScrollHandler);
		}
		private function onStageScrollHandler(e:Event):void
		{
			_customScrollBar.update();
			this.removeEventListener(Event.ADDED_TO_STAGE,	onStageScrollHandler);
		}
		public function set selectClipSwitch($param:Boolean):void
		{
			if($param){
				TweenMax.to(_selectClip, 0.3, {y:30, autoAlpha:1, ease:Quart.easeOut});
			}else{
				TweenMax.to(_selectClip, 0.3, {y:30, autoAlpha:0, ease:Quart.easeOut});	
			}
			_openCheck = $param;
		}
		
		public function set boxID($num:int):void{_num=$num};
		public function get boxID():int{return _num};
		public function get currentType():String{return _activetxt.text};
		public function set colorNoChange($colorNoChange:Boolean):void{_colorNoChange = $colorNoChange};
		
		private function setLayOut():void
		{
			this._inputClip	=	this.getChildByName('input_mc') 		as MovieClip;
			this._activetxt	=	this.getChildByName('active_txt')		as TextField;
			this._mask		= 	this.getChildByName('mask_mc')			as Sprite;
			this._selectClip= 	this.getChildByName('select_mc')		as MovieClip;
		}
		private function init():void
		{
			isCreateRoomMode = false;
			this._inputClip.addEventListener(MouseEvent.CLICK,	onMouseClickHandler);
		}
		
		private function defaultSetting():void
		{
			this._inputClip.buttonMode = true;
			
			var textformat_letterspacing:TextFormat = new TextFormat();
			textformat_letterspacing.letterSpacing 	= -0.6;
			
			this._activetxt.defaultTextFormat 		= textformat_letterspacing;
			
			this._activetxt.mouseWheelEnabled 		= false;
			this._activetxt.mouseEnabled 			= false;
			
			this._selectClip.alpha 					= 0;
			this._selectClip.visible 				= false;
			
			this.addEventListener(Event.ADDED_TO_STAGE, onStageHandler);
		}
		
		private function onStageHandler(e:Event):void
		{
			_customScrollBar = new CustomScrollBar(stage, _selectClip.contents_mc, _selectClip.mask_mc, _selectClip.bar_mc,  _selectClip.btn_mc, true, true, true, false, .15, 5);
			this.removeEventListener(Event.ADDED_TO_STAGE, onStageHandler);
		}
		
		private function onMouseClickHandler(e:MouseEvent):void
		{
			//trace('>>>'+this._num);
			if(_openCheck){
				selectClipSwitch = false;
			}else{
				dispatchEvent(new ENumber(boxID,	ENumber.EVENT_NUMBER));
			}
		}
		private var _selectedID:int = 100;
		
		private function onChildClickHandler(e:MouseEvent):void
		{
			
			//trace('click id is '+e.currentTarget._id);
			if(e.currentTarget.textIn == "선택안함"){
				if(_activetxt.text != "선택과목 "+String(_num +1)) dispatchEvent(new EDataObject({id:_selectedID, selected:false}, EDataObject.EVENT_DATAOBJECT));
				
				this._activetxt.text = "선택과목 "+String(_num +1);
				this._activetxt.textColor = 0xcccccc;
				if(!_colorNoChange) selectedBox = false;
				_selectedID = 100;
			}else{
				
				this._activetxt.text = e.currentTarget.textIn;
				if(!_colorNoChange) this._activetxt.textColor = 0xffffff;
				if(!_colorNoChange) selectedBox = true;
				
				
				//방만들기 팝업에서만 적용
				if(_isCreateRoomMode == true){
					_selectedID = e.currentTarget._id;
					dispatchEvent(new EDataObject({id:_selectedID, box:boxID}, EDataObject.EVENT_DATAOBJECT));
					selectClipSwitch = false;
					//this._inputClip.addEventListener(MouseEvent.CLICK,	onMouseClickHandler);
					return;
				}

				if(_selectedID != 100) dispatchEvent(new EDataObject({id:_selectedID, selected:false}, EDataObject.EVENT_DATAOBJECT));
				_selectedID = e.currentTarget._id;
				dispatchEvent(new EDataObject({id:_selectedID, selected:true}, EDataObject.EVENT_DATAOBJECT));
			}
			
			selectClipSwitch = false;
			this._inputClip.addEventListener(MouseEvent.CLICK,	onMouseClickHandler);
			
		}
		private function onChildOverHandler(e:MouseEvent):void
		{
			e.currentTarget.addEventListener(MouseEvent.ROLL_OUT,		onChildOutHandler);
			e.currentTarget.overEffect = true;
		}
		private function onChildOutHandler(e:MouseEvent):void
		{
			e.currentTarget.removeEventListener(MouseEvent.ROLL_OUT,	onChildOutHandler);
			e.currentTarget.overEffect = false;
		}
		private function set selectedBox($bool:Boolean):void
		{
			if($bool){
				//selected.
				this._inputClip.l_mc.gotoAndStop(2);
				this._inputClip.r_mc.gotoAndStop(2);
				this._inputClip.c_mc.gotoAndStop(2);	
				
			}else{
				//unselected.
				this._inputClip.l_mc.gotoAndStop(1);
				this._inputClip.r_mc.gotoAndStop(1);
				this._inputClip.c_mc.gotoAndStop(1);	
				
			}
		}
		
		
		//data 입력.
		private var _childObj:Array;
		public function set viewOn($param:Array):void
		{
			if(_childObj != null) resetSelectBox();
			if(_childObj == null) _childObj = new Array();
			
			for(var i:int = 0 ; i < $param.length ; i++){
				_childObj[i] 		= new PopupSelectItem();
				if( i == 0) _childObj[i].lineVisible	= false;
				_childObj[i]._id						= $param[i].id;
				_childObj[i].y 					 		= i*24;
				_childObj[i].textWidth 					= _viewWidth - 5;
				_childObj[i].lineWidth					= _viewWidth;
				_childObj[i].textIn						= $param[i].name;
				if($param.length >4){
					_childObj[i].lineWidth = _viewWidth - 26;
					
				}else{
					_childObj[i].lineWidth = _viewWidth;
				}
				
				_childObj[i].addEventListener(MouseEvent.CLICK,			onChildClickHandler);
				_childObj[i].addEventListener(MouseEvent.ROLL_OVER,		onChildOverHandler);
				this._selectClip.contents_mc.addChild(_childObj[i]);
			}
		
			selectClipSwitch = true;
			
			if($param.length < 4){
				_selectClip.bg_mc.height = ($param.length) * 24 + 5;
			}else{
				_selectClip.bg_mc.height = (4) * 24 + 5;
			}
			_selectClip.mask_mc.height = _selectClip.bg_mc.height;
			_selectClip.y = 30 - _selectClip.bg_mc.height;
			_selectClip.bar_mc.x = _selectClip.btn_mc.x = _selectClip.bg_mc.width - 19;	
			
			_inputClip.addEventListener(MouseEvent.CLICK,				onMouseClickHandler);
			
			scrollbarReset();
		}
		
		
		
		
		private function resetSelectBox():void
		{
			if(_childObj.length > 0){
				for (var i:int = 0 ; i < _childObj.length ; i++) {
					if (this._childObj[i] !== null && this._childObj[i] !== undefined) {
						this._selectClip.contents_mc.removeChild(this._childObj[i]);
					}
					this._childObj[i] = null;
				}
				_childObj.splice(0, _childObj.length-1);
			} 
		}
		
		
		public function set isCreateRoomMode ($bool : Boolean):void
		{
			_isCreateRoomMode = $bool; 
		}
	
	}
}